package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import org.ilovecode.control.Scrollbar;
	import org.ilovecode.screen.Screen;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class AboutProjectApartments extends Screen {
		
		//{ region Stage elements
		
		public var scrollbar:Scrollbar;
		public var content:MovieClip;
		public var scrollMask:MovieClip;
		public var background:MovieClip;
		public var closeButton:SimpleButton;
		public var downloadPDFButton:SimpleButton;
		
		//} endregion
		
		private const CONTENT_OFFSET:int = 100;
		public var zoomWindow:ZoomWindow;
		private var inZoomView:Boolean = false;
		
		public function AboutProjectApartments() {
			super(ScreenName.ABOUT_PROJECT, ScreenGroup.APARTMENTS, "aboutProjectApartments");
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			closeButton.visible = false;
			zoomWindow.visible = false;
			downloadPDFButton.visible = false;
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			scrollbar.addEventListener(Event.SCROLL, update);
			scrollbar.addEventListener(Event.COMPLETE, update);
			content.addEventListener(MouseEvent.CLICK, zoomButtonClick);
			closeButton.addEventListener(MouseEvent.CLICK, closeButtonClick);
			zoomWindow.addEventListener(Event.COMPLETE, updateVisibility);
			downloadPDFButton.addEventListener(MouseEvent.CLICK, downloadPDFButton_Click);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			scrollbar.removeEventListener(Event.SCROLL, update);
			scrollbar.removeEventListener(Event.COMPLETE, update);
			content.removeEventListener(MouseEvent.CLICK, zoomButtonClick);
			closeButton.removeEventListener(MouseEvent.CLICK, closeButtonClick);
			zoomWindow.removeEventListener(Event.COMPLETE, updateVisibility);
			downloadPDFButton.removeEventListener(MouseEvent.CLICK, downloadPDFButton_Click);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case Event.SCROLL:
					scrollbar.addEventListener(Event.ENTER_FRAME, scrollContent);
					break;
				case Event.COMPLETE:
					scrollbar.removeEventListener(Event.ENTER_FRAME, scrollContent);
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		//{ region Handlers
		
		/*private function scrollbarScroll(event:Event):void {
			scrollbar.addEventListener(Event.ENTER_FRAME, scrollContent);
		}
		
		private function scrollbarComplete(event:Event):void {
			scrollbar.removeEventListener(Event.ENTER_FRAME, scrollContent);
		}*/
		
		private function scrollContent(event:Event):void {
			var scrollingArea:Number = content.height + CONTENT_OFFSET - scrollMask.height;
			var contentPosition:Number = - scrollbar.getScrollPositionRatio() * scrollingArea;
			TweenLite.to(content, 0.2, { y:contentPosition, ease:Linear.easeOut } );
		}
		
		private function zoomButtonClick(event:MouseEvent):void {
			inZoomView = true;
			updateVisibility();
			
			var s:Situacija = new Situacija();
			
			var w:Number = (s.width > background.width) ? background.width : s.width;
			var h:Number = (s.height > background.height) ? background.height : s.height;
			zoomWindow.init(s, w + 10, h);
			zoomWindow.focusRect = false;
			zoomWindow.x = (background.width - zoomWindow.width) / 2;
			zoomWindow.y = (background.height - zoomWindow.height) / 2;
			this.stage.focus = zoomWindow;
			
		}
		
		private function downloadPDFButton_Click(event:MouseEvent):void {
			var url:String = "http://www.centar-bundek.hr/Content/PDF/Situacija/Centar-Bundek-Situacija.pdf";
			navigateToURL(new URLRequest(url), "_blank");
		}
		
		protected function closeButtonClick(event:MouseEvent):void
		{
			inZoomView = false;
			zoomWindow.clear();
		}
		
		private function updateVisibility(event:Event=null):void
		{
			zoomWindow.visible = closeButton.visible = downloadPDFButton.visible = inZoomView;
			scrollbar.visible = content.visible = scrollMask.visible = !inZoomView;
			
		}
		
		//} endregion
		
	}

}